package com.zygh.questionnairesurvey.server;


import com.zygh.questionnairesurvey.entity.UserInfo;
import com.zygh.questionnairesurvey.entity.UserMenu;
import com.zygh.questionnairesurvey.mapper.UserInfoMapper;
import com.zygh.questionnairesurvey.mapper.UserRoleMapper;
import com.zygh.questionnairesurvey.security.CustomUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class JwtUserDetailsService implements UserDetailsService {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private UserMenuService userMenuService;


    @Autowired
    private HttpServletRequest request;

    @Override
    public UserDetails loadUserByUsername(String value) throws UsernameNotFoundException {

        UserInfo userInfoAccount = userInfoMapper.findUserByUsername(value);

        if (userInfoAccount == null) {
            throw new UsernameNotFoundException(String.format("No user found with username '%s'.", value));
        }
        List<UserMenu> roles = userMenuService.listAlls();

        return new CustomUserDetails(userInfoAccount, roles);
    }
}
